"""This script will convert the JSON data of Statistics Topics Index, 
which is stored in CouchDB, to RDF.
sti_couchdb_dump.JSON is the JSON file that was used with this script.
"""


import json
import xml.etree.ElementTree as xml


infile = open("sti_couchdb_dump.JSON", 'r')
db = json.load(infile)
infile.close()
docs = [r['doc'] for r in db['rows']]
for d in docs:
	del [d['_rev']]
types = list(set([d['doc_type'] for d in docs if 'doc_type' in d])) #types = ['Topic', 'Course', 'Publisher', 'Book', 'Person']


#person
doc_type = "Person"
person_docs = [d for d in docs if ('doc_type' in d) and (d['doc_type']==doc_type)] 
person_etrees = []
for doc in person_docs:
	root = xml.Element('rdf:Description')
	root.attrib['rdf:type'] = doc['doc_type']
	doc_id = xml.Element('person:id')
	doc_id.text = doc['_id']
	root.append(doc_id)
	if 'first_name' in doc:
		first_name = xml.Element('person:first_name')
		first_name.text = doc['first_name']
		root.append(first_name)
	if 'last_name' in doc:
		last_name = xml.Element('person:last_name')
		last_name.text = doc['last_name']
		root.append(last_name)
	if 'knowledge' in doc:
		knowledge = xml.Element('person:knowledge')
		root.append(knowledge)
		knowledge_list_container = xml.Element('rdfs:Container')
		knowledge.append(knowledge_list_container)
		for k in doc['knowledge']:
			knowledge_list_member = xml.Element('rdfs:member')
			knowledge_list_container.append(knowledge_list_member)
			knowledge_container = xml.Element('rdfs:Container')
			knowledge_list_member.append(knowledge_container)
			if 'topic' in k:
				topic = xml.Element('key:topic')
				topic.attrib['rdf:resource'] = k['topic']
				knowledge_container.append(topic)
			if 'source' in k:
				source = xml.Element('key:source')
				source.text = k['source']
				knowledge_container.append(source)
	if 'sources' in doc:
		sources = xml.Element('person:sources')
		root.append(sources)
		sources_list_container =  xml.Element('rdfs:Container')
		sources.append(sources_list_container)
		for s in doc['sources']:
			m = xml.Element('rdfs:member')
			m.text = s
			sources_list_container.append(m)
	person_etrees.append(root)
		
		


#publisher
doc_type = "Publisher"
pub_docs = [d for d in docs if ('doc_type' in d) and (d['doc_type']==doc_type)] 
pub_etrees = []
for doc in pub_docs:
	root = xml.Element('rdf:Description')
	root.attrib['rdf:type'] = doc['doc_type']
	doc_id = xml.Element('publisher:id')
	doc_id.text = doc['_id']
	root.append(doc_id)
	if 'name' in doc:
		name = xml.Element('publisher:name')
		name.text = doc['name']
		root.append(name)
	if 'state_province' in doc:
		state_province = xml.Element('publisher:state_province')
		state_province.text = doc['state_province']
		root.append(state_province)
	if 'city' in doc:
		city = xml.Element('publisher:city')
		city.text = doc['city']
		root.append(city)
	if 'country' in doc:
		country = xml.Element('publisher:country')
		country.text = doc['country']
		root.append(country)
	if 'address' in doc:
		address = xml.Element('publisher:address')
		address.text = doc['address']
		root.append(address)
	if 'website' in doc:
		website = xml.Element('publisher:website')
		website.text = doc['website']
		root.append(website)
	pub_etrees.append(root)
		
		
#Course
doc_type = "Course"
course_docs = [d for d in docs if ('doc_type' in d) and (d['doc_type']==doc_type)] 
course_etrees = []
for doc in course_docs:
	root = xml.Element('rdf:Description')
	root.attrib['rdf:type'] = doc['doc_type']
	doc_id = xml.Element('course:id')
	doc_id.text = doc['_id']
	root.append(doc_id)
	if 'name' in doc:
		name = xml.Element('course:name')
		name.text = doc['name']
		root.append(name)
	if 'description' in doc:
		description = xml.Element('course:description')
		description.text = doc['description']
		root.append(description)
	if 'semester' in doc:
		semester = xml.Element('course:semester')
		semester.text = doc['semester']
		root.append(semester)
	if 'lectures' in doc:
		lectures = xml.Element('course:lectures')
		root.append(lectures)
		lectures_list_container = xml.Element('rdfs:Container')
		lectures.append(lectures_list_container)
		for lec in doc['lectures']:
			lectures_list_member = xml.Element('rdfs:member')
			lectures_list_container.append(lectures_list_member)
			lecture_container = xml.Element('rdfs:Container')
			lectures_list_member.append(lecture_container)
			if 'title' in lec:
				title = xml.Element('key:title')
				title.text = lec['title']
				lecture_container.append(title)
			if 'lecture_num' in lec:
				lecture_num = xml.Element('key:lecture_num')
				lecture_num.text = str(lec['lecture_num'])
				lecture_container.append(lecture_num)
			if 'source' in lec:
				source = xml.Element('key:source')
				source.text = lec['source']
				lecture_container.append(source)
			if 'topics' in lec:	
				topics = xml.Element('key:topics')
				lecture_container.append(topics)
				topics_container = xml.Element('rdfs:Container')
				topics.append(topics_container)
				for top in lec['topics']:
					m = xml.Element('rdfs:member')
					m.attrib['rdf:resource'] = top
					topics_container.append(m)
	if 'lecturers' in doc:
		lecturers = xml.Element('course:lecturers')
		root.append(lecturers)
		lecturers_list_container =  xml.Element('rdfs:Container')
		lecturers.append(lecturers_list_container)
		for p in doc['lecturers']:
			m = xml.Element('rdfs:member')
			m.attrib['rdf:resource'] = p
			lecturers_list_container.append(m)
	if 'books' in doc:
		books = xml.Element('course:books')
		root.append(books)
		books_list_container =  xml.Element('rdfs:Container')
		books.append(books_list_container)
		for b in doc['books']:
			m = xml.Element('rdfs:member')
			m.attrib['rdf:resource'] = b
			books_list_container.append(m)
	course_etrees.append(root)
		




#Topic
doc_type = "Topic"
topic_docs = [d for d in docs if ('doc_type' in d) and (d['doc_type']==doc_type)] 
topic_etrees = []
for doc in topic_docs:
	root = xml.Element('rdf:Description')
	root.attrib['rdf:type'] = doc['doc_type']
	doc_id = xml.Element('topic:id')
	doc_id.text = doc['_id']
	root.append(doc_id)
	if 'name' in doc:
		name = xml.Element('topic:name')
		name.text = doc['name']
		root.append(name)
	if 'categories' in doc:
		categories = xml.Element('topic:categories')
		root.append(categories)
		categories_list_container = xml.Element('rdfs:Container')
		categories.append(categories_list_container)
		for c in doc['categories']:
			categories_list_member = xml.Element('rdfs:member')
			categories_list_container.append(categories_list_member)
			category_container = xml.Element('rdfs:Container')
			categories_list_member.append(category_container)
			if 'category' in c:
				category_name = xml.Element('key:category_name')
				category_name.text = c['category']
				category_container.append(category_name)
			if 'sources' in c:
				sources = xml.Element('key:sources')
				category_container.append(sources)
				sources_list_container = xml.Element('rdfs:Container')
				sources.append(sources_list_container)
				for s in c['sources']:
					m = xml.Element('rdfs:member')
					sources_list_container.append(m)
					m_container = xml.Element('rdfs:Container')
					m.append(m_container)
					if 'type' in s:
						tp = xml.Element('key:type')
						tp.text = s['type']
						m_container.append(tp)
					if 'reference' in s:
						ref = xml.Element('key:reference')
						ref.text = s['reference']
						m_container.append(ref)
			if 'associations' in c:					
				associations = xml.Element('key:associations')
				category_container.append(associations)
				associations_list_container = xml.Element('rdfs:Container')
				associations.append(associations_list_container)
				for assoc in c['associations']:
					m = xml.Element('rdfs:member')
					associations_list_container.append(m)
					m_container = xml.Element('rdfs:Container')
					m.append(m_container)
					if 'type' in assoc:
						tp = xml.Element('key:type')
						tp.text = assoc['type']
						m_container.append(tp)
					if 'target' in assoc:
						target = xml.Element('key:target')
						target.attrib['rdf:resource'] = assoc['target']
						m_container.append(target)
					if 'reference' in assoc:
						ref = xml.Element('key:reference')
						ref.text = assoc['reference']
						m_container.append(ref)
	topic_etrees.append(root)
		
#
#Book
def entry_to_rdf(entry, container):
	if 'name' in entry:
		name = xml.Element('key:name')
		name.text = entry['name']
		container.append(name)
	if 'heirarchy' in entry:
		hierarchy = xml.Element('key:hierarchy')
		hierarchy.text = entry['hierarchy']
		container.append(hierarchy)
	if 'order' in entry:
		order = xml.Element('key:order')
		order.text = str(entry['order'])
		container.append(order)
	if 'page' in entry:
		page = xml.Element('key:page')
		page.text = entry['page']
		container.append(page)
	if 'topics' in entry:
		topics = xml.Element('key:topics')
		container.append(topics)
		topics_list_container = xml.Element('rdfs:Container')
		topics.append(topics_list_container)
		for top in entry['topics']:
			m = xml.Element('rdfs:member')
			m.attrib['rdf:resource'] = top
			topics_list_container.append(m)
	if 'children' in entry:
		children = xml.Element('key:children')
		container.append(children)
		children_list_container = xml.Element('rdfs:Container')
		children.append(children_list_container)
		for c in entry['children']:
			m = xml.Element('rdfs:member')
			children_list_container.append(m)
			m_container = xml.Element('rdfs:Container')
			m.append(m_container)
			entry_to_rdf(c, m_container)
			


doc_type = "Book"
book_docs = [d for d in docs if ('doc_type' in d) and (d['doc_type']==doc_type)] 
book_etrees = []
for doc in book_docs:
	root = xml.Element('rdf:Description')
	root.attrib['rdf:type'] = doc['doc_type']
	doc_id = xml.Element('book:id')
	doc_id.text = doc['_id']
	root.append(doc_id)
	if 'title' in doc:
		title = xml.Element('book:title')
		title.text = doc['title']
		root.append(title)
	if 'isbn' in doc:
		isbn = xml.Element('book:isbn')
		isbn.text = doc['isbn']
		root.append(isbn)
	if 'year' in doc:
		year = xml.Element('book:year')
		year.text = doc['year']
		root.append(year)
	if 'edition' in doc:
		edition = xml.Element('book:edition')
		edition.text = doc['edition']
		root.append(edition)
	if 'publisher' in doc:
		publisher = xml.Element('book:publisher')
		publisher.attrib['rdf:resource'] = doc['publisher']
		root.append(publisher)
	if 'publisher_listing_url' in doc:
		publisher_listing_url = xml.Element('book:publisher_listing_url')
		publisher_listing_url.text = doc['publisher_listing_url']
		root.append(publisher_listing_url)
	if 'image_url' in doc:
		image_url = xml.Element('book:image_url')
		image_url.text = doc['image_url']
		root.append(image_url)
	if 'authors' in doc:
		authors = xml.Element('book:authors')
		root.append(authors)
		autors_list_container = xml.Element('rdfs:Container')
		authors.append(autors_list_container)
		for au in doc['authors']:
			m = xml.Element('rdfs:member')
			m.attrib['rdf:resource'] = au
			autors_list_container.append(m)
	if 'toc' in doc:
		toc = xml.Element('book:toc')
		root.append(toc)
		toc_list_container = xml.Element('rdfs:Container')
		toc.append(toc_list_container)
		for entry in doc['toc']:
			toc_list_member = xml.Element('rdfs:member')
			toc_list_container.append(toc_list_member)
			entry_container = xml.Element('rdfs:Container')
			toc_list_member.append(entry_container)
			entry_to_rdf(entry, entry_container)
	book_etrees.append(root)




rdf = xml.Element('rdf:RDF')
rdf.attrib['xmlns:rdf'] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
rdf.attrib['xmlns:rdfs'] = "http://www.w3.org/2000/01/rdf-schema#"
rdf.attrib['xmlns:key'] = "http://sti/keys/"
rdf.attrib['xmlns:person'] = "http://sti/persons/"
rdf.attrib['xmlns:publisher'] = "http://sti/publishers/"
rdf.attrib['xmlns:course'] = "http://sti/courses/"
rdf.attrib['xmlns:topic'] = "http://sti/topics/"
rdf.attrib['xmlns:book'] = "http://sti/books/"

all_etrees = [person_etrees, pub_etrees, course_etrees, topic_etrees, book_etrees]
for etree in all_etrees:
	for e in etree:
		rdf.append(e)

ofile = open('sti_rdf_dump.rdf', 'w')
xml.ElementTree(rdf).write(ofile, encoding='UTF-8')
ofile.close()



